# uos_uwsgi_exporter Makefile

# 变量定义
BINARY_NAME=uos_uwsgi_exporter
BUILD_DIR=build
BINARY_PATH=$(BUILD_DIR)/bin/$(BINARY_NAME)
VERSION?=$(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
COMMIT?=$(shell git rev-parse HEAD 2>/dev/null || echo "unknown")
DATE?=$(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
LDFLAGS=-ldflags "-X main.version=$(VERSION) -X main.commit=$(COMMIT) -X main.date=$(DATE)"

# 默认目标
.PHONY: all
all: build

# 创建构建目录
$(BUILD_DIR)/bin:
	mkdir -p $(BUILD_DIR)/bin

# 构建项目
.PHONY: build
build: $(BUILD_DIR)/bin
	go build $(LDFLAGS) -o $(BINARY_PATH) .

# 构建并安装
.PHONY: install
install: build
	cp $(BINARY_PATH) /usr/local/bin/
	mkdir -p /etc/uwsgi_exporter
	cp config/example.yaml /etc/uwsgi_exporter/config.yaml

# 运行测试
.PHONY: test
test:
	go test -v ./...

# 运行测试并生成覆盖率报告
.PHONY: test-coverage
test-coverage:
	go test -v -coverprofile=coverage.out ./...
	go tool cover -html=coverage.out -o coverage.html

# 清理构建文件
.PHONY: clean
clean:
	rm -rf $(BUILD_DIR)
	rm -f coverage.out coverage.html

# 格式化代码
.PHONY: fmt
fmt:
	go fmt ./...

# 代码检查
.PHONY: lint
lint:
	golangci-lint run

# 运行项目
.PHONY: run
run: build
	./$(BINARY_PATH)

# 开发模式运行
.PHONY: dev
dev:
	go run . -sockets /tmp/uwsgi.sock

# 构建Docker镜像
.PHONY: docker-build
docker-build:
	docker build -t $(BINARY_NAME):$(VERSION) .

# 运行Docker容器
.PHONY: docker-run
docker-run:
	docker run -p 9070:9070 $(BINARY_NAME):$(VERSION)

# 生成依赖图
.PHONY: deps
deps:
	go mod graph | dot -Tpng -o deps.png

# 更新依赖
.PHONY: deps-update
deps-update:
	go get -u ./...
	go mod tidy

# 显示帮助
.PHONY: help
help:
	@echo "可用的目标:"
	@echo "  build        - 构建项目"
	@echo "  install      - 构建并安装到系统"
	@echo "  test         - 运行测试"
	@echo "  test-coverage- 运行测试并生成覆盖率报告"
	@echo "  clean        - 清理构建文件"
	@echo "  fmt          - 格式化代码"
	@echo "  lint         - 代码检查"
	@echo "  run          - 构建并运行项目"
	@echo "  dev          - 开发模式运行"
	@echo "  docker-build - 构建Docker镜像"
	@echo "  docker-run   - 运行Docker容器"
	@echo "  deps         - 生成依赖图"
	@echo "  deps-update  - 更新依赖"
	@echo "  help         - 显示此帮助信息" 